﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Symbonix.NorSoNet.Framework;
using Symbonix.NorSoNet.Security.BLL.Membership;
using Symbonix.NorSoNet.Framework.BLL;
using System.Net.Mail;
using Symbonix.Library.Mail;
using System.Web.Security;
using Symbonix.NorSoNet.Security.DAL.Repositories;
using Symbonix.Library.Web.Security.DAL.Entities;
using Symbonix.NorSoNet.Security.DAL.Entities;


namespace Symbonix.NorSoNet.Security.BLL
{
    public class SecurityMailHelper : FrameworkMailHelper
    {
        protected SecurityMailHelper() { }

        protected internal static SecurityMailHelper GetInstance() { return new SecurityMailHelper(); }

        public void SendRegistrationMail(IUser user)
        {
            IEmailTemplate emailTemplate = Framework.BLL.ModuleManager.FrameworkFactory.GetEmailTemplate();
            emailTemplate = emailTemplate.Load((int)(Symbonix.NorSoNet.Security.BLL.Options.EmailTemplates.RegistrationMail));

            ExtendedUserRepository extendedUserRepository = new ExtendedUserRepository();
            User siteUser = extendedUserRepository.GetByPrimaryKey(user.Id);

            NameWithEmail[] receivers = new NameWithEmail[1];
            receivers[0] = new NameWithEmail(user.FirstName + " " + user.LastName, user.Email);

            Symbonix.NorSoNet.Framework.BLL.Configuration.ISettings setting = Symbonix.NorSoNet.Framework.BLL.ModuleManager.FrameworkFactory.GetSettings();



            string enctyptedUsername = Symbonix.Library.Utility.Security.Encrypt(user.UserName, siteUser.PasswordSalt, true);

            string url = string.Format("{0}/activate-account.aspx?key={1}&email={2}", setting.SiteUrl, enctyptedUsername, user.Email);
            string body = emailTemplate.Body.Replace("[%%URL%%]", url); ; // make the body by processing template

            base.SendMail(emailTemplate.Subject, body, receivers);
        }

        public void SendPasswordRecoveryMail(string userName)
        {
            IEmailTemplate emailTemplate = Framework.BLL.ModuleManager.FrameworkFactory.GetEmailTemplate();
            emailTemplate = emailTemplate.Load((int)(Symbonix.NorSoNet.Security.BLL.Options.EmailTemplates.PasswordRecoveryMail));

            MembershipUser member = System.Web.Security.Membership.Provider.GetUser(userName, false);
            ExtendedUserRepository eur = new ExtendedUserRepository();
            User user = eur.GetByPrimaryKey(Int32.Parse(member.ProviderUserKey.ToString()));


            NameWithEmail[] receivers = new NameWithEmail[1];
            receivers[0] = new NameWithEmail(user.FirstName + " " + user.LastName, user.Email);
            VerificationCodeData verfication = new VerificationCodeData();
            verfication.ForUserID = user.UserID;
            verfication.Code = Guid.NewGuid();
            VerificationCodeDataRepository varificationRepository = new VerificationCodeDataRepository();
            varificationRepository.Insert(verfication);
            // verfication.

            string enctyptedUsername = Symbonix.Library.Utility.Security.Encrypt(userName, user.PasswordSalt, true);
            string url = string.Format("{0}/RecoverPassword.aspx.aspx?key={1}&email={2}", SiteSettings.SiteUrl, verfication.Code, user.Email);

            string body = emailTemplate.Body.Replace("[%%URL%%]", url); ;
            body = body.Replace("[%%SITE_NAME%%]", SiteSettings.SiteUrl);
            body = body.Replace("[%%UserName%%]", userName);


            base.SendMail(emailTemplate.Subject, body, receivers);
        }
    }
}
